package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackStatMapper extends BaseMapper<TrackStat> {


    /**
     * 更新声音统计
     * @param trackStatMqVo
     */
    @Update("UPDATE track_stat \n" +
            "SET stat_num=stat_num+#{vo.count} ,update_time = NOW() \n" +
            "WHERE track_id = #{vo.trackId} AND stat_type = #{vo.statType} AND is_deleted = 0")
    void updateTrackStat(@Param("vo") TrackStatMqVo trackStatMqVo);

    /**
     * 获取声音统计信息
     * /api/album/trackInfo/getTrackStatVo/{trackId}
     */
    @Select("SELECT \n" +
            "MAX(IF(ts.stat_type='0701',stat_num,0)) playStatNum,\n" +
            "MAX(IF(ts.stat_type='0702',stat_num,0)) collectStatNum,\n" +
            "MAX(IF(ts.stat_type='0703',stat_num,0)) praiseStatNum,\n" +
            "MAX(IF(ts.stat_type='0704',stat_num,0)) commentStatNum\n" +
            "FROM track_stat ts WHERE ts.track_id = #{trackId} and ts.is_deleted = 0;")
    TrackStatVo gselectTrackStatVo(Long trackId);
}
